package savi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import savi.classes.Accounts;
import savi.classes.Events;

@SuppressWarnings("serial")
public class EventsServlet extends HttpServlet {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private Accounts accounts = Accounts.inst();

	// OPERATIONS
	public static final String ADD_EVENT = "add_event";
	public static final String UPDATE_EVENT = "update_event";
	public static final String DELETE_EVENT = "delete_event";
	public static final String JOIN_EVENT = "join_event";
	public static final String LEAVE_EVENT = "leave_event";
	public static final String GET_EVENT_DESC = "get_event_desc";
	public static final String GET_FUTURE_EVENT_SUGGESTIONS = "get_future_event_suggestions";
	public static final String GET_FUTURE_EVENTS_JOINED = "get_future_event_joined";
	public static final String GET_PAST_EVENTS = "get_past_events";
	public static final String GET_ALL_USER_MODULE_EVENTS = "get_all_user_module_events";
	public static final String GET_USER_CREATED_EVENTS = "get_user_created_events";

	// PARAMETERS
	public static final String OPERATION = "operation";
	public static final String EVENT_ID = "eventID";
	public static final String OWNER_ID = "ownerID";
	public static final String MODULE_ID = "moduleID";
	public static final String TYPE = "type";
	public static final String LOCATION = "location";
	public static final String START_TIME = "startTime";
	public static final String END_TIME = "endTime";
	public static final String DESCRIPTION = "description";

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		this.request = req;
		this.response = resp;

		String operation = request.getParameter(OPERATION);

		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");

		if (operation.equals(ADD_EVENT)) {
			addEvent();
		} else if (operation.equals(DELETE_EVENT)) {
			deleteEvent();
		} else if (operation.equals(UPDATE_EVENT)) {
			updateEvent();
		} else if (operation.equals(GET_EVENT_DESC)) {
			getEventDescription();
		} else if (operation.equals(JOIN_EVENT)) {
			joinEvent();
		} else if (operation.equals(LEAVE_EVENT)) {
			leaveEvent();
		}
		if (operation.equals(GET_ALL_USER_MODULE_EVENTS)) {
			getAllUserModuleEvents();
		} else if (operation.equals(GET_FUTURE_EVENTS_JOINED)) {
			getFutureEventsJoined();
		} else if (operation.equals(GET_FUTURE_EVENT_SUGGESTIONS)) {
			getFutureEventSuggestions();
		} else if (operation.equals(GET_PAST_EVENTS)) {
			getPastEvents();
		} else if (operation.equals(GET_USER_CREATED_EVENTS)) {
			getUserCreatedEvents();
		} else {
			System.out.println("Unknown command\n");
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		// to be implemented

	}

	private Date convertStringToDate(String dateString) {
		Date date = new Date();

		try {
			SimpleDateFormat sdfSource = new SimpleDateFormat(
					"dd/MM/yyyy hh:mm");
			date = sdfSource.parse(dateString);
		} catch (ParseException pe) {
			System.out.println("Parse Exception : " + pe);
		}

		return date;
	}

	public void addEvent() throws IOException {
		Events events = Events.inst();
		String moduleID = request.getParameter(MODULE_ID);
		String type = request.getParameter(TYPE);
		String location = request.getParameter(LOCATION);
		Date startTime = convertStringToDate(request.getParameter(START_TIME));
		Date endTime = convertStringToDate(request.getParameter(END_TIME));
		String description = request.getParameter(DESCRIPTION);
		System.out.println("moduleID:" + moduleID);
		events.addEvent(moduleID, type, location, startTime, endTime,
				description);
	}

	public void deleteEvent() throws IOException {
		Events events = Events.inst();
		int eventID = Integer.parseInt(request.getParameter(EVENT_ID));
		events.deleteEvent(eventID);
	}

	public void updateEvent() throws IOException {
		Events events = Events.inst();
		// int eventID = ;
		String ownerID = accounts.getUserName();
		String moduleID = request.getParameter(MODULE_ID);
		String type = request.getParameter(TYPE);
		String location = request.getParameter(LOCATION);
		Date startTime = convertStringToDate(request.getParameter(START_TIME));
		Date endTime = convertStringToDate(request.getParameter(END_TIME));
		String description = request.getParameter(DESCRIPTION);
		// events.updateEvent(eventID, ownerID, moduleID,
		// type, location, startTime, endTime, description);
	}

	public String getEventDescription() throws IOException {
		Events events = Events.inst();
		int eventID = Integer.parseInt(request.getParameter(EVENT_ID));
		return events.getEventDesc(eventID);
	}

	public void joinEvent() throws IOException {
		Events events = Events.inst();
		String eventID = request.getParameter(EVENT_ID);
		events.joinEvent(eventID);
	}

	public void leaveEvent() throws IOException {
		Events events = Events.inst();
		int eventID = Integer.parseInt(request.getParameter(EVENT_ID));
		events.leaveEvent(eventID);
	}

	public void getAllUserModuleEvents() throws IOException {
		Events events = Events.inst();
		String result = events.getAllUserModuleEvents();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}

	public void getFutureEventsJoined() throws IOException {
		Events events = Events.inst();
		String result = events.getFutureEventsJoinedByStudent();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}

	public void getFutureEventSuggestions() throws IOException {
		Events events = Events.inst();
		String result = events.getFutureEventSuggestionsForStudent();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}

	public void getPastEvents() throws IOException {
		Events events = Events.inst();
		String result = events.getPastEventsOfStudentModules();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}

	public void getUserCreatedEvents() throws IOException {
		Events events = Events.inst();
		String result = events.getAllUserCreatedEvents();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}
}
